#!/bin/bash
source /usr/local/easycontrol/easycontrol.conf

dbclients=/partition/easycontrol/clients
_DBASE="$dbclients"
EASYCONTROLPATH=/usr/local/easycontrol




#-------------------------------------------------------------------------------
JAVASCRIPTS () {

#POPUP
#para usar chame:
#<A HREF="javascript:popUp('PAGINA')"> texto </A>

cat << CLEOF
<SCRIPT LANGUAGE="JavaScript">
<!-- Idea by:  Nic Wolfe -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=300');");
}
// End -->
</script>
CLEOF
}





#-------------------------------------------------------------------------------
#tabelalimpa=/tmp/lista-cliente-$$
#grep -v ^# $tabelaclientes > $tabelalimpa


#estas variaveis servem pra ajustar os campos, de maneira que se você quiser
#alterar algo conforme necessario, basta ajustar aqui a ordem de cada 
#campo. Isso pode ser util caso venha a querer adicionar um novo campo,
#sem precisar editar todo o script

_code=1
_name=2
_address=3
_tel1=4
_tel2=5
_email=6
_obs=7
_payday=8
_login=9
_password=10
_auth=11
_mac=12
_ip=13
_gateway=14
_bandadown=15
_bandaup=16
_status=17

SEP=:

#-------------------------------------------------------------------------------



field () {



while test -n "$1"
do
	case "$1" in	
	-r | --reference)
	use_reference=1
	reference="$2"
	;;
	-f | --fields)
	_list="$2"
	
	;;	
	-d | --database)
	_database="$2"
	esac

shift
done


fields_list=$(echo "$_list"|
sed "s/code/$_code/g;
s/name/$_name/g;
s/address/$_address/g;
s/tel1/$_tel1/g;
s/tel2/$_tel2/g;
s/email/$_email/g;
s/obs/$_obs/g;
s/payday/$_payday/g;
s/login/$_login/g;
s/password/$_password/g;
s/auth/$_auth/g;
s/mac/$_mac/g;
s/auth/$_auth/g;
s/mac/$_mac/g;
s/ip/$_ip/g;
s/gateway/$_gateway/g;
s/bandadown/$_bandadown/g;
s/bandaup/$_bandaup/g;
s/status/$_status/g")

#para nao digitar toda hora o nome do banco, basta definir uma variavel
#chamada _DBASE

[ -z "$_database" ] && _database=$_DBASE


if [ "$use_reference" = "1" ];
then
	grep $reference "$_database" | cut -d : -f $fields_list 
else
	cut -d "$SEP" -f "$fields_list" "$_database"

fi


}

#-------------------------------------------------------------------------------

validate () {

case $1 in
	text)
	
echo -e $(sed "s/[áàâäÁÀÂÄãªÃ]/a/g;
s/[éèêëÉÈÊË&]/e/g;
s/[íìîïÍÌÎÏ]/i/g;
s/[óòôöÓÒÔÖõ°ºÕ]/o/g;
s/[úùûüÚÙÛÜ]/u/g;
s/[çÇ]/c/g") | tr -d '$`'

;;
	number)
	
	tr -d a-zA-ZáàâäÁÀÂÄãªÃéèêëÉÈÊË\&íìîïÍÌÎÏóòôöÓÒÔÖõ°ºÕúùûüÚÙÛÜçÇ
	esac
	

}


#-------------------------------------------------------------------------------




#-------------------------------------------------------------------------------
CALL_CONFIG () {
cat << CLEOF

<table class=maintable border=0 cellspacing=1 width=100%>

CLEOF


for CONFIGS in /usr/local/easycontrol/formats/* ;
do
cat <<EOF
<tr>
<td>
EOF


sed -n /BEGIN/,/END/p $CONFIGS | sed "/BEGIN/d;/END/d" | tr -d \#


cat <<EOF
</tr>
</td>
EOF

done

cat << FIM_TABELA
</table>
FIM_TABELA


}

#-------------------------------------------------------------------------------



CALL_RELOAD () {

cat <<EOF

<table class=maintable border=0 cellspacing=1 width=100%>

EOF

for CONFIGS in /usr/local/easycontrol/formats/* ; do

cat << CLEOF
<tr>
<td valign="top">
CLEOF


echo '<pre>'
$CONFIGS mount
$CONFIGS reload
echo '</pre>'



cat << CLEOF
</td>
</tr>
CLEOF


done

cat << CLEOF

</table>
CLEOF

rm /tmp/sccli.needsave

exit
}


#-------------------------------------------------------------------------------

CALL_BACKUP () {

FILEBACKUP="clients_$(date +%Y-%m-%d_%H-%M).tgz"
cd /partition/easycontrol/
tar -cf /usr/local/easycontrol/http/admin/"$FILEBACKUP" clients

cat << CLEOF
<BODY onLoad="javascript:popUp('/$FILEBACKUP')">
CLEOF
USERS_TABLE

}
#-------------------------------------------------------------------------------

UPDATE_STATUS () {


[ -n ${FORM_status} ] && valor_status="${FORM_status}" || valor_status="$(field status)"
[ -n ${FORM_mac} ] && valor_mac="${FORM_mac}" || valor_mac="$(field mac)"




case $valor_status in
	0)
	UNBLOCK_MAC $valor_mac 2> /dev/null
	;;
	1)
	UNBLOCK_MAC $valor_mac 2> /dev/null
	PREBLOCK_MAC $valor_mac
	
	;;
	2)
	UNBLOCK_MAC $valor_mac 2> /dev/null
	BLOCK_MAC $valor_mac

	esac

}


#-------------------------------------------------------------------------------

UNBLOCK_MAC () {

/usr/sbin/iptables -t nat -D PREROUTING -p tcp -m mac --mac-source $1 \
-j DNAT --to-destination $LOCAL_IPADDR:${BLOCK_PORT}

/usr/sbin/iptables -t nat -D PREROUTING -p tcp -m mac --mac-source $1 \
-j DNAT --to-destination $LOCAL_IPADDR:${PREBLOCK_PORT}

}

#-------------------------------------------------------------------------------

BLOCK_MAC () {


/usr/sbin/iptables -t nat -I PREROUTING -p tcp -m mac --mac-source $1 \
-j DNAT --to-destination $LOCAL_IPADDR:${BLOCK_PORT}


}

#-------------------------------------------------------------------------------
PREBLOCK_MAC () {

/usr/sbin/iptables -t nat -I PREROUTING -p tcp -m mac --mac-source $1 -j DNAT --to-destination $LOCAL_IPADDR:${PREBLOCK_PORT}

}

#-------------------------------------------------------------------------------

VERIFY_CONNECTION () {

fgrep -c $1 /proc/net/ip_conntrack


}
#-------------------------------------------------------------------------------
